

#import "UIControl+xib.h"
#import <objc/runtime.h>
#import "UIColor+category_CCKit.h"

static const UIColor *_enabledTextColor = nil;
static const UIColor *_enabledBackColor = nil;
static const UIColor *_disEnabledTextColor = nil;
static const UIColor *_disEnabledBackColor = nil;
@implementation UIControl (xib)
@dynamic  disEnabledTextColor;
@dynamic disEnabledBackHexColor;
@dynamic textHexColor;
@dynamic disEnabledBackColor;
@dynamic disEnabledTextHexColor;
@dynamic enabledTextColor;
@dynamic enabledBackColor;
- (instancetype)init
{
    if (self = [super init])
    {
        [self addObserver:self forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self addObserver:self forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}
- (nullable instancetype)initWithCoder:(NSCoder *)coder
{
    if (self = [super initWithCoder:coder])
    {
        [self addObserver:self forKeyPath:@"enabled" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"enabled"])
    {
        BOOL enabled = [change valueForKey:keyPath];
        if (enabled)
        {
            self.enabledBackColor = self.enabledBackColor;
            self.enabledTextColor = self.enabledTextColor;
        }
        else
        {
            self.disEnabledBackColor = self.disEnabledBackColor;
            self.disEnabledTextColor = self.disEnabledTextColor;
        }
    }
}
- (void)setupTextColor:(UIColor *)color
{
    if ([self isKindOfClass:[UIButton class]])
    {
        UIButton *button = (UIButton *)self;
        [button setTitleColor:color forState:UIControlStateNormal];
        button.titleLabel.textColor = color;
    }
    else if ([self isKindOfClass:[UILabel class]])
    {
        UILabel *label = (UILabel *)self;
        label.textColor = color;
    }
    else if ([self isKindOfClass:[UITextField class]]){
        UITextField *input = (UITextField *)self;
        input.textColor = color;
    }else if ([self isKindOfClass:[UITextView class]]){
        UITextView *input = (UITextView *)self;
        input.textColor = color;
    }
    //其他控件control待续
    return;
}
- (void)setupBackColor:(UIColor *)color
{
    self.backgroundColor = color;
}
- (void)setDisEnabledTextHexColor:(NSString *)disEnabledTextHexColor
{
    self.disEnabledTextColor = ColorHex(disEnabledTextHexColor);
}
- (void)setDisEnabledBackHexColor:(NSString *)disEnabledBackHexColor
{
    self.disEnabledBackColor = ColorHex(disEnabledBackHexColor);
}
- (void)setTextHexColor:(NSString *)textHexColor
{
    self.enabledTextColor = ColorHex(textHexColor);
}
- (void)setEnabledTextColor:(UIColor *)enabledTextColor
{
    if (!enabledTextColor)
        return;
    objc_setAssociatedObject(self, &_enabledTextColor, enabledTextColor, OBJC_ASSOCIATION_RETAIN);
    if (self.enabled)
        [self setupTextColor:enabledTextColor];
}
- (void)setDisEnabledTextColor:(UIColor *)disEnabledTextColor
{
    if (!disEnabledTextColor)
        return;
    objc_setAssociatedObject(self, &_disEnabledTextColor, disEnabledTextColor, OBJC_ASSOCIATION_RETAIN);
    if (self.enabled == NO)
        [self setupTextColor:disEnabledTextColor];
}
- (void)setEnabledBackColor:(UIColor *)enabledBackColor
{
    if (!enabledBackColor)
        return;
    objc_setAssociatedObject(self, &_enabledBackColor, enabledBackColor, OBJC_ASSOCIATION_RETAIN);
    if (self.enabled)
        [self setupBackColor:enabledBackColor];
}
- (void)setDisEnabledBackColor:(UIColor *)disEnabledBackColor
{
    if (!disEnabledBackColor)
        return;
    objc_setAssociatedObject(self, &_disEnabledBackColor, disEnabledBackColor, OBJC_ASSOCIATION_RETAIN);
    if (self.enabled == NO)
        [self setupBackColor:disEnabledBackColor];
}
#pragma mark == get
- (UIColor *)enabledTextColor
{
    return objc_getAssociatedObject(self, &_enabledTextColor);
}
- (UIColor *)disEnabledTextColor
{
    return objc_getAssociatedObject(self, &_disEnabledTextColor);
}
- (UIColor *)enabledBackColor
{
    return objc_getAssociatedObject(self, &_enabledBackColor);
}
- (UIColor *)disEnabledBackColor
{
    return objc_getAssociatedObject(self, &_disEnabledBackColor);
}
@end
